package com.example.tibetanmedicinechest.controller;


import com.example.tibetanmedicinechest.model.Code;
import com.example.tibetanmedicinechest.model.Content;
import com.example.tibetanmedicinechest.model.Result;
import com.example.tibetanmedicinechest.service.IContentService;
import com.example.tibetanmedicinechest.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 编程笨蛋
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    IContentService iContentService;
    @GetMapping
    public Result selectAllContentByUserId(Integer user_id){
        List<Map<String, Object>> maps = iContentService.selectContentByUserId(user_id);
        if(maps.size()!=0){
            return ResultUtil.getSuccess(maps);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"该用户没有进行任何评论");
        }
    }
    @GetMapping("/selectByMedicineId/{medicine_id}")
    public Result selectByMedicineId(@PathVariable Integer medicine_id){
        List<Map<String, Object>> maps = iContentService.selectByMedicineId(medicine_id);
        if(maps.size()!=0){
            return ResultUtil.getSuccess(maps);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"该药品没有人进行评论");
        }
    }

    @PostMapping
    public Result addContent(@RequestBody Content content){
        System.out.println(content);
        boolean flag = iContentService.addContent(content);
        if(flag){
            return ResultUtil.getSuccess(null);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"发表评论失败");
        }
    }
}

